package de.tabuma.pm.security.controller;

import de.tabuma.lib.*;
import java.util.*;
import java.util.logging.*;
import java.io.*;
import java.lang.reflect.*;
import javax.servlet.*;
import javax.servlet.http.*;
import de.tabuma.pm.security.*;
import de.tabuma.lib.contentmanagement.*;
import de.tabuma.lib.contentmanagement.entity.*;
import de.tabuma.ssoclient.entity.*;
import de.tabuma.ssoclient.logik.*;
import com.google.appengine.api.users.*;
/**
 * @author $Author: alefen $
 * @version $Revision: 1.1.1.1.6.1 $
 * @since $Date: 2010/01/13 10:19:31 $
 */
public class UserController extends tabumacontroller {

    private static Logger log = Logger.getLogger(UserController.class.getName());

    public UserController(String servlet, Hashtable parameter) {
        super(servlet, parameter);
    }

    @Override
    public void navigate(ServletContext context, HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
       
        try {

            if(request.getParameter(de.tabuma.pm.constants.SECURITY_PRIVIDER_ATTRIBUTE_MANAGEMENT)!=null) {
                Hashtable parameter = (Hashtable) this.getParameter().clone();
                parameter.put(de.tabuma.pm.constants.SECURITY_PROVIDER_USER_MANAGEMENT, "true");
                parameter.put(de.tabuma.pm.constants.SECURITY_PROVIDER_USER_PARAM+de.tabuma.lib.contentmanagement.constants.id, request.getParameter(de.tabuma.pm.constants.SECURITY_PROVIDER_USER_PARAM+de.tabuma.lib.contentmanagement.constants.id));

                AttributeController attributeController = new AttributeController(this.getServlet(), parameter);
                attributeController.navigate(context, request, response, out);
                return;
            }

            String id = request.getParameter(de.tabuma.pm.constants.SECURITY_PROVIDER_PARAM+de.tabuma.lib.contentmanagement.constants.id);
            int intId = Integer.valueOf(id).intValue();

            String groupId = request.getParameter(de.tabuma.pm.constants.SECURITY_PROVIDER_GROUP_PARAM+de.tabuma.lib.contentmanagement.constants.id);
            int groupIdInt = Integer.valueOf(groupId);


            out.println("<div class=\"contentContent\">");
            out.println("<p class=\"contentContentHeader\">User Management</p>");
            out.println("</div>");

            String entityName = "User";
            String userName = "user";
            String userPassword = "password";

            Content content = new Content();
            Vector<Field> editableFields = new Vector<Field>();
            editableFields.add(de.tabuma.ssoclient.entity.User.class.getDeclaredField("name"));
            editableFields.add(de.tabuma.ssoclient.entity.User.class.getDeclaredField("password"));
            content.FormContent.setEditableFields(editableFields);

            Vector<Integer> editableTypes = new Vector<Integer>();
            editableTypes.add(new Integer(de.tabuma.lib.contentmanagement.constants.TYPE_STRING));
            editableTypes.add(new Integer(de.tabuma.lib.contentmanagement.constants.TYPE_STRING));
            content.FormContent.setEditableFieldTypes(editableTypes);

            content.FormContent.setEntityClass(de.tabuma.ssoclient.entity.User.class);
            content.FormContent.setEntityName(entityName);

            String ssoId = SecurityProviderInstanceFacade.getSingleSignOnId(intId);
            UserFacade uf = new UserFacade();
            uf.setSsoId(ssoId);
            uf.setGroupId(groupIdInt);

            content.FormContent.setFacadeObject(uf);

            UserService userService = UserServiceFactory.getUserService();
            com.google.appengine.api.users.User user = userService.getCurrentUser();
            String projectId = request.getParameter(de.tabuma.pm.constants.projectId);
            content.ListContent.setListParameterValue(""+projectId);

            content.GeneralContent.setParamName(de.tabuma.pm.constants.SECURITY_PROVIDER_USER_PARAM);

            Vector<Field> listableFields = new Vector<Field>();
            listableFields.add(de.tabuma.ssoclient.entity.User.class.getDeclaredField("name"));
            content.ListContent.setListableFields(listableFields);

            String attributeManagement = "attribute management";;

            Hashtable<String,Hashtable<String,String>> additionalListFields = new Hashtable<String,Hashtable<String,String>>();
            Hashtable<String,String> parameterHash = (Hashtable<String,String>) this.getParameter().clone();
            parameterHash.put(de.tabuma.pm.constants.SECURITY_PRIVIDER_ATTRIBUTE_MANAGEMENT, "true");
            additionalListFields.put(attributeManagement, parameterHash);
            content.ListContent.setAdditionalListFields(additionalListFields);

            content.GeneralContent.setServlet(this.getServlet());

            Hashtable hash = (Hashtable) this.getParameter().clone();
            hash.put(de.tabuma.pm.constants.SECURITY_PROVIDER_USER_MANAGEMENT, "true");
            hash.put(de.tabuma.pm.constants.SECURITY_PROVIDER_PARAM+de.tabuma.lib.contentmanagement.constants.id, id);
            content.GeneralContent.setParameter(hash);

            Hashtable<Field, String> displayFields = new Hashtable<Field, String>();
            displayFields.put(de.tabuma.ssoclient.entity.User.class.getDeclaredField("name"), userName);
            displayFields.put(de.tabuma.ssoclient.entity.User.class.getDeclaredField("password"), userPassword);
            content.FormContent.setDisplayedName(displayFields);
            
            content.GeneralContent.setUser(user.getEmail());
            out.println(content.navigate(request));

            out.println("<div class=\"contentContent\">");
            out.println(this.getFormBeginn(null));
            out.println(this.getHiddenAsAttribute());
            out.println("<input type=\"submit\" name=\"FormBeginn\" value=\"back\" class=\"button\"/>");
            out.println(this.getFormEnd());
            out.println("</div>");
        }
        catch(Exception e) {
            String warning = e.getMessage()+"/"+e.getCause()+"/"+e.getClass()+"\n";
            for(int i=0;i<e.getStackTrace().length;i++) {
                warning += e.getStackTrace()[i]+"\n";
            }
            log.warning(warning);
        }
    }
}